<?php
require_once(APPPATH.'views/components/page.php');
/**
 * Admin Controller
 */
class Admin extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('user_model', 'user');
		$this->load->library('form_validation');
		//$this->lang->load('form_validation', 'vietnamese');
	}
	
	public function index()
	{
		$error = $this->session->userdata('error');
		$this->session->unset_userdata('error');
		
		if ($error == ERROR_NONE || $error == NULL)
		{
			$userdata = $this->session->userdata('userdata');
		
			if ($userdata == NULL)
			{
				$error = UNAUTHORIZED_ERROR;
			}
			else
			{
				if ($userdata['admin'])
					$error = ERROR_NONE;
			}
		}
		
		$data['rerror'] = $error;
		$data['username'] = $this->session->userdata('username');
		$this->session->unset_userdata('username');
		$this->load->view('admin_view', $data);
	}
	
	public function login()
	{
		$error = ERROR_NONE;
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		if (strlen($username) > 0 && strlen($password) > 0)
		{
			$hashed = $this->hashData($password);
		
			$user = $this->user->getUserByUsername($username);
			$error = $this->session->userdata('error');
				
			if (strcasecmp($user->getPassword(), DEFAULT_PASSWORD) == 0)
				$error = PASSWORD_CHANGE_REQUIRED;
			else if (strcasecmp($hashed, $user->getPassword()) == 0)
			{
				$this->setUserData($user);
			}
			else {
				$error = AUTHORIZE_FAILED;
			}
		}
		else
		{
			$error = AUTHORIZE_FAILED;
		}
		
		
		$this->session->set_userdata('username', $username);
		$this->session->set_userdata('error', $error);
		redirect('admin');
	}
	
	private function setUserData($user)
	{
		$this->session->unset_userdata('userdata');
		$sessionData = array('userid' 	=> $user->getId(),
								'username'	=> $user->getUsername(),
								'groupid'	=> $user->getGroup()->getId(),
								'admin'		=> $user->getGroup()->isAdmin()
								);
		$this->session->set_userdata('userdata', $sessionData);
	}
	
	public function logout()
	{
		$error = UNAUTHORIZED_ERROR;
		
		$this->session->unset_userdata('userdata');
		
		$this->session->set_userdata('error', $error);
		redirect('admin', 'refresh');
	}
	
	private function hashData($string)
	{
		$output = hash('sha256', SALT.$string.SALT);
		
		return $output;
	}
	
	public function changepassword()
	{
		$error = ERROR_NONE;
		
		$userdata = $this->session->userdata('userdata');
		$userid = $userdata['userid'];
		$username = $this->input->post('username');
		
		$this->form_validation->set_message('required', '%s không được để trống.');
		$this->form_validation->set_message('matches', '%s và %s không giống nhau.');
		$this->form_validation->set_rules('newpassword', 'Mật khẩu mới', 'required|matches[confirmpassword]');
		$this->form_validation->set_rules('confirmpassword', 'Nhập lại mật khẩu', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('admin/admin_detail');
		}
		else {
			$newPass = $this->input->post('newpassword');
			
			if ($userid > 0)
				$user = $this->user->getUserById($userid);
			else
				$user = $this->user->getUserByUsername($username);
			
			$hashed = $this->hashData($newPass);
			$user->setPassword($hashed);
			
			$error = $this->user->updateUser($user->getId(), $user);
			
			if ($error = ERROR_NONE)
				$this->setUserData($user);
			
			$this->session->set_userdata('error', $error);
			redirect('admin', 'refresh');
		}
	}
	
	public function view()
	{
		$this->load->view('admin/admin_detail');
	}
}
/* End of file admin.php */
/* Location: ./application/controllers/admin.php */